using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.Permissions;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;

namespace Joy.Erp.BasicArchives.ItemMasters
{
    [Authorize]
    public partial class UnitGroupAppService : BasicArchivesAppService, IUnitGroupAppService
    {
        protected IUnitGroupRepository UnitGroupRepository => LazyServiceProvider.LazyGetRequiredService<IUnitGroupRepository>();
        protected UnitGroupManager UnitGroupManager => LazyServiceProvider.LazyGetRequiredService<UnitGroupManager>();

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<UnitGroupDto> GetAsync(Guid id)
        {
            var entity = await UnitGroupRepository.GetAsync(id, true);
            return ObjectMapper.Map<UnitGroup, UnitGroupDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<AuxiliaryUnitDto> GetChildAsync(Guid id)
        {
            var child = await UnitGroupRepository.GetChildAsync(id, true);
            return ObjectMapper.Map<AuxiliaryUnit, AuxiliaryUnitDto>(child);
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<UnitGroupDto> FindAsync(string filter)
        {
            var entity = await UnitGroupRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<UnitGroup, UnitGroupDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<UnitGroupDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await UnitGroupRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<UnitGroup, UnitGroupDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<PagedResultDto<UnitGroupDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await UnitGroupRepository.GetCountAsync(input.Filter, true);
            var list = await UnitGroupRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<UnitGroupDto>(
                totalCount,
                ObjectMapper.Map<List<UnitGroup>, List<UnitGroupDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<PagedResultDto<UnitGroupDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<UnitGroup, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<UnitGroup>(input.FilterGroup);
            }

            var totalCount = await UnitGroupRepository.GetCountAsync(expression, true);
            var list = await UnitGroupRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<UnitGroupDto>(
                totalCount,
                ObjectMapper.Map<List<UnitGroup>, List<UnitGroupDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Query)]
        public async Task<PagedResultDto<AuxiliaryUnitDto>> QueryChildrenAsync(FilterGroupRequestDto input)
        {
            Expression<Func<AuxiliaryUnit, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<AuxiliaryUnit>(input.FilterGroup);
            }

            var totalCount = await UnitGroupRepository.GetChildrenCountAsync(expression, true);
            var list = await UnitGroupRepository.GetChildrenPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<AuxiliaryUnitDto>(
                totalCount,
                ObjectMapper.Map<List<AuxiliaryUnit>, List<AuxiliaryUnitDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Create)]
        public async Task<UnitGroupDto> CreateAsync(UnitGroupCreateDto input)
        {
            var entity = ObjectMapper.Map<UnitGroupCreateDto, UnitGroup>(input);

            await UnitGroupManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Update)]
        public async Task<UnitGroupDto> UpdateAsync(Guid id, UnitGroupUpdateDto input)
        {
            var entity = await UnitGroupRepository.GetAsync(id, true);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            await UnitGroupRepository.EnsureCollectionLoadedAsync(entity, x => x.Children);
            ObjectMapper.Map(input, entity);

            await UnitGroupManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.UnitGroup.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await UnitGroupManager.DeleteAsync(id);
        }
    }
}
